package org.jsola.hr.common;

import com.alibaba.excel.EasyExcel;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsola.exception.ParamException;
import org.jsola.file.entity.FileVersionDO;
import org.jsola.hr.exception.HrException;
import org.springframework.core.io.FileSystemResource;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author jp_cyl
 * @description
 * @date 2019/11/6
 */
public class ExcelChange {

    /**
    *方法描述 将上传的文件保存在本地，得到文件路径
    *@author jp_cyl
    *@date 2020/1/4 11:09
    *@param excel
    *@returns java.util.List<org.jsola.pms.dto.ExcelHouseDTO>
    */
    public static String getCarportFile (MultipartFile excel) {
        String tempPath = System.getProperty("java.io.tmpdir");
        File dir = new File(tempPath);
        if(!dir.exists()){
            dir.mkdirs();
        }
        if(!dir.canRead()){
            System.out.println("没有读权限");
        }
        if(!dir.canWrite()){
            System.out.println("没有写权限");
        }
        if(!dir.canExecute()){
            System.out.println("没执行权限");
        }
        if(excel == null){
            throw new HrException("上传的文件为空");
        }
        String[] split = excel.getOriginalFilename().split("\\.");  //.是特殊字符，需要转义！！！！！
        String newFileName = "carportExcel";
        File excelFile = dir;
        String filePath = "";
        FileInputStream fis;
        if ( "xls".equals(split[1])){
            filePath = tempPath+File.separator+newFileName+"."+"xls";
            excelFile = new File(filePath);
            excelFile.setExecutable(true);
            excelFile.setReadable(true);
            excelFile.setWritable(true);
            try {
                excel.transferTo(excelFile);
            } catch (IOException e) {
                throw new HrException("xls文件转化错误");
            }
        }else if ("xlsx".equals(split[1])){
            filePath = tempPath+File.separator+newFileName+"."+"xlsx";
            excelFile = new File(filePath);
            excelFile.setExecutable(true);
            excelFile.setReadable(true);
            excelFile.setWritable(true);
            try {
                excel.transferTo(excelFile);
            } catch (IOException e) {
                throw new HrException("xlsx文件转化错误");
            }
        }else{
            throw new ParamException("不支持该文件格式");
        }
        return filePath;
    }


}
